import { Map, View } from "ol";
import {defaults as defaultInteractions} from 'ol/interaction'
//@ts-ignore
import { defaultControls } from './controls.js';// 地图上面的控件 
//@ts-ignore
import { MapLayer } from "./layers.js";
import { Select } from 'ol/interaction'
import { doubleClick } from 'ol/events/condition'

import Field from "./feature/field.ts";


/** 
 * 创建地图的参数
 * @typedef {object} CreateMapOption
 * @property {string} [target] 地图容器， div的 id
 * @property {Array<number>} [center] 地图中线点
 * @property {string} [name] 地图名称 
 * @property {Boolean} [loadField=true] 加载地块
 * @property {Boolean} [loadPartition=true] 加载分区
 * @property {Boolean} [loadDevice=true] 加载分区
 */


/**
 * map地图的包装类，代表每个页面的地图对象。所有与地图交互都会通过此接口
 * @property {import ('ol').Map} omap
 */
class MapWrapper {
  /**
   * 
   * @param { import ('ol').Map } map 地图对象
   * @param {string} name 地图名称
   * @param {MapLayer} layer 地图图层工具类
   */
  //@ts-ignore
  constructor(map, layer, name) {
    /**
     * ol.Map 地图对象
     * @type { import ('ol').Map } map
     */
    //@ts-ignore
    this.omap = map;
    /**
     * 地图中的图层
     * @type {MapLayer}
     */
    //@ts-ignore
    this.layers = layer;
    /**
     * 地图名称
     * @public
     * @type {string}
     */
    //@ts-ignore
    this.name = name;
  }
}

interface MapOptions {
  target?:string,
  center?:[],
  name?:string
}

/**
 * 地图管理类，提供创建、获取当前地图等功能
 */
class MapManager {

  maps:[];
  current:null;

  constructor() {

    /**
     * 所有创建的map对象
     * @type Array<MapWrapper> 
     * @private
     */
    this.maps = [];
    /**
     * 当前地图对象
     * @type {MapWrapper}
     * @public
     */
    this.current = null;

  }

  /**
   * 创建一个地图对象
   * @param {CreateMapOption} opts
   * @public
   */
  createMap(opts:MapOptions) {
    var target = opts.target !== undefined ? opts.target : 'map';
    var center = opts.center !== undefined ? opts.center : [114.67729, 38.54869];
    var name = opts.name !== undefined ? opts.name : target;

   

    var mapLayers = new MapLayer();

    //创建地图完成
    var omap = new Map({
      target: target,
      controls: defaultControls(),
      layers: mapLayers.getAllLayers(),
      interactions: defaultInteractions({doubleClickZoom:false}),
      view: new View({
        projection: 'EPSG:4326',
        center: center,
        zoom: 16,
        minZoom: 5,
        maxZoom: 19,
      })
    });

    let mapWrapper = new MapWrapper(omap, mapLayers, name);
    //@ts-ignore
    this.current = mapWrapper;
    //@ts-ignore
    this.maps.push(mapWrapper);

    //初始化数据
    this.initMapData(opts);

      

    setTimeout(() => {
      omap.updateSize();  
    }, 20);
  }

  /**
   * 初始化地图数据
   * @param {CreateMapOption} opts 
   * @private
   */
  //@ts-ignore
  initMapData(opts) {
    
    

    //初始化地块列表
    Field.initData();

    
  }


  //@ts-ignore
  animate(params) {
    //@ts-ignore
    this.current.omap.getView().animate(params);
  }


  

  //@ts-ignore
  on(type, lis) {
    //@ts-ignore
    this.current.omap.on(type, lis);
  }

  getMap() {
    //@ts-ignore
    return this.current.omap;
  }
  
  getLayers() {
    //@ts-ignore
    return this.current.layers;
  }
 

  /**
   * 初始化双击选中Feature时间
   */
  doubleClickSelect = new Select({
    condition: doubleClick,
    style: null
  });

  isDoubleClickRemoved = false;

}

 



 

var rightPanelOpened = false;



/**
* 实例化一个地图管理类
*/
var mapManager = new MapManager();


export {
  mapManager, 
   
  rightPanelOpened
}